﻿using LiveCharts;
using LiveCharts.Configurations;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModbusTest.Models
{
    public class ItemModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        //从站地址
        public int SlaveId { get; set; }
        //温度
        private double _temperature;
        public double Temperature
        {
            get { return _temperature; }
            set
            {
                _temperature = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Temperature"));

                DateTime dt = DateTime.Now;
                TempValues.Add(new LvcModel { Value = value, DateTime = dt });
                if (TempValues.Count > 11)
                    TempValues.RemoveAt(0);

                UpdateRange(dt);
            }
        }
        //湿度
        private double _humidity;

        public double Humidity
        {
            get { return _humidity; }
            set
            {
                _humidity = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Humidity"));

                DateTime dt = DateTime.Now;
                HumiValues.Add(new LvcModel { Value = value, DateTime = dt });
                if (HumiValues.Count > 11)
                    HumiValues.RemoveAt(0);
            }
        }

        //livecharts的Y轴数据
        public ChartValues<LvcModel> TempValues { get; set; } =
            new ChartValues<LvcModel>();
        public ChartValues<LvcModel> HumiValues { get; set; } =
            new ChartValues<LvcModel>();


        public ItemModel()
        {
            CartesianMapper<LvcModel> mapper = Mappers.Xy<LvcModel>()
                .X(model => model.DateTime.Ticks)
                .Y(model => model.Value);
            Charting.For<LvcModel>(mapper);

            XFormatter = new Func<double, string>(x => new DateTime((long)x).ToString("mm:ss"));

            UpdateRange(DateTime.Now);
        }

        public Func<double, string> XFormatter { get; set; }
        private double _axisMin;

        public double AxisMin
        {
            get { return _axisMin; }
            set
            {
                _axisMin = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("AxisMin"));
            }
        }
        private double _axisMax;

        public double AxisMax
        {
            get { return _axisMax; }
            set
            {
                _axisMax = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("AxisMax"));
            }
        }

        private void UpdateRange(DateTime now)
        {
            AxisMax = now.Ticks;
            AxisMin = now.Ticks - TimeSpan.FromSeconds(9).Ticks;
        }

        public double AxisUnit { get; set; } = TimeSpan.TicksPerSecond;
        public double AxisStep { get; set; } = TimeSpan.FromSeconds(1).Ticks;
    }
}
